<div>
    <span class="dropdown" style="display:inline-block">
        <a id="" class="dropdown-toggle btn btn-white  waves-effect" data-toggle="dropdown" href="javascript:void(0)">
            <stub>设备：</stub><span class="caret" id="device-span"> </span>
        </a>
        <ul class="dropdown-menu" style="left: 0px; right: inherit;">
            @foreach($cabinets as $k => $v)
                <li class="dropdown-item">
                    <a href="javascript:void(0)" onclick="setDevice({{ $k }}, '{{ $v }}')">{{ $v }}</a>
                </li>
            @endforeach
        </ul>
    </span>
</div>
<div class="x-y" style="display: none;">
    <span class="dropdown" style="display:inline-block">
        <a id="" class="dropdown-toggle btn btn-white  waves-effect" data-toggle="dropdown" href="javascript:void(0)">
            <stub>X轴：</stub><span class="caret" id="x-span"> </span>
        </a>
        <ul id="x-index" class="dropdown-menu" style="left: 0px; right: inherit;">
            
        </ul>
    </span>
    <span class="dropdown" style="display:inline-block">
        <a id="" class="dropdown-toggle btn btn-white  waves-effect" data-toggle="dropdown" href="javascript:void(0)">
            <stub>Y轴：</stub><span class="caret" id="y-span"> </span>
        </a>
        <ul id="y-index" class="dropdown-menu" style="left: 0px; right: inherit;">
            
        </ul>
    </span>
    <input type="hidden" name="x-val">
    <input type="hidden" name="y-val">
    <input type="hidden" name="cabinetId">
</div>
<div id="echart" style="width: 100%;height:400px;"></div>
<script src="/echarts/echarts.common.min.js"></script>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('echart'));
    var chartXData = {!! $xData !!};
    var chartYData = {!! $yData !!};
    var chartTooltip = {trigger: 'axis'};
    var chartYAxis = {type: 'value'};
    var chartTooltip = {trigger: 'axis'};

    var option = {
            tooltip: {
                trigger: 'axis',
                formatter: function (params) {
                    return params[0].marker+params[0].data.value+" ("+params[0].data.time+")"
                },
            },
            xAxis: {
                type: 'category',
                data: chartXData
            },
            yAxis: chartYAxis,
            series: [{
                data: chartYData,
                type: 'line',
                smooth: true
            }]
        };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    function setDevice(id, name) {
        $('#device-span').html(name);
        $('input[name=cabinetId]').val(id);
        $('.x-y').show();
        let url = "{{ URL::asset('admin/getPointsByCabinetId') }}";
        let data = {
            _token: '{{ csrf_token() }}',
            id: id
        }
        $.ajax({
            type : "POST",
            url : url,
            data : data,
            success : function(result) {
                if (result.code == 1) {
                    let xhtml = '';
                    let yhtml = '';
                    for(var alias in result.data) {
                        xhtml += '<li class="dropdown-item">';
                        yhtml += '<li class="dropdown-item">';
                        
                        xhtml += '<a href="javascript:void(0)" onclick="setIndex(\'x\',\''+alias+'\',\''+result.data[alias]+'\')">'+result.data[alias]+'<\/a><\/li>';
                        yhtml += '<a href="javascript:void(0)" onclick="setIndex(\'y\',\''+alias+'\',\''+result.data[alias]+'\')">'+result.data[alias]+'<\/a><\/li>';
                    }
                    $('#x-index').html(xhtml);
                    $('#y-index').html(yhtml);
                }
            },
            //请求失败，包含具体的错误信息
            error : function(e){
                layer.close(e);
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }

    function setIndex(index, alias, name){
        if (index == 'x') {
            $('input[name=x-val]').val(alias);
            $('#x-span').html(name);
        }
        if (index == 'y') {
            $('input[name=y-val]').val(alias);
            $('#y-span').html(name);
        }
        let x = $('input[name=x-val]').val();
        let y = $('input[name=y-val]').val();
        let cabinetId = $('input[name=cabinetId]').val();

        if (x && y && cabinetId) {
            let url = "{{ URL::asset('admin/getChartByPoint') }}";
            let data = {
                _token: '{{ csrf_token() }}',
                x: x,
                y:y,
                cabinetId:cabinetId
            }
            Dcat.loading();
            $.ajax({
                type : "POST",
                url : url,
                data : data,
                success : function(result) {
                    Dcat.loading(false);
                    if (result.code == 1) {
                        myChart.setOption({
                            tooltip: chartTooltip,
                            xAxis: {
                                type: 'category',
                                data: result.data.xData
                            },
                            yAxis: chartYAxis,
                            series: [{
                                data: result.data.yData,
                                type: 'line',
                                smooth: true
                            }]
                        });
                    }
                },
                //请求失败，包含具体的错误信息
                error : function(e){
                    Dcat.loading(false);
                    layer.close(e);
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });
        }


// Dcat.loading();
// Dcat.loading(false);
return;
        console.log(alias);
        myChart.setOption({
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: chartYAxis,
            series: [{
                data: [820, 932, 901, 934, 1290, 1330, 1320],
                type: 'line',
                smooth: true
            }]
        });
    }
</script>